package com.global.college.views.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.global.college.R;
import com.global.college.ui.activity.HsLagelDetailActivity;
import com.global.college.utils.ScreenUtils;


/**
 * 隐私权限弹窗
 */
public class PermissionAndPrivacyDialogTip extends Dialog implements View.OnClickListener {

    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvCancel;
    private TextView tvOk;
    LinearLayout llAction;
    private Activity context;
    private LayoutInflater inflater;



    public PermissionAndPrivacyDialogTip(Activity context, OnCallBack onCallBack) {
        super(context, R.style.dialog);
        this.context = context;
        mOnCallBack = onCallBack;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.dialog_permission_and_privacy_tip, null);
        setContentView(view);
        initView(view);
        initData();
        setCanceledOnTouchOutside(false);
        Window window = this.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ScreenUtils.getScreenWidth(context)
                - ScreenUtils.getScreenWidth(context) / 4;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        window.setWindowAnimations(R.style.dialog);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    private void initView(View view) {
        tvTitle = view.findViewById(R.id.tv_title);
        tvContent = view.findViewById(R.id.tv_content);
        tvCancel = view.findViewById(R.id.tv_cancel);
        tvOk = view.findViewById(R.id.tv_Ok);
        setContentColor();
    }

    private void setContentColor() {
        String strContent = tvContent.getText().toString();
        //SpannableStringBuilder类支持textview改变部分字体颜色和单独设置点击事件，需要的参数只有一个，就是需要显示的文字内容就行，我这里直接从控件获取文本内容了
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(strContent);
        //第一个出现的位置
        final int start = strContent.indexOf("《");
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                //隐私政策 点击事件
                getContext().startActivity(new Intent(getContext(), HsLagelDetailActivity.class).putExtra("type", "1"));
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                //设置文件颜色
                ds.setColor(getContext().getResources().getColor(R.color.color_orange));
                // 去掉下划线
                ds.setUnderlineText(false);
            }

        }, start, start + 6, 0);
        //最后一个出现的位置
        final int end = strContent.lastIndexOf("《");
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                //用户注册协议  击事件
                getContext().startActivity(new Intent(getContext(), HsLagelDetailActivity.class).putExtra("type", "0"));
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                //设置文件颜色
                ds.setColor(getContext().getResources().getColor(R.color.color_orange));
                // 去掉下划线
                ds.setUnderlineText(false);
            }

        }, end, end + 11, 0);
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
        tvContent.setText(ssb, TextView.BufferType.SPANNABLE);

    }

    private void initData() {
        tvCancel.setOnClickListener(this);
        tvOk.setOnClickListener(this);
    }

    public void setLoginAgree(){
        tvOk.setText("同意");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                if (null != mOnCallBack) {
                    mOnCallBack.callBack(0);
                    dismiss();
                }
                break;
            case R.id.tv_Ok:
                if (null != mOnCallBack) {
                    mOnCallBack.callBack(1);
                    dismiss();
                }
                break;
        }
    }

    public interface OnCallBack {
        void callBack(int param);
    }

    private OnCallBack mOnCallBack;

}
